package org.begincode.resource.service.impl;

import java.util.List;

import org.begincode.core.mapper.ResourceMapper;
import org.begincode.core.model.Resource;
import org.begincode.core.paginator.domain.PageList;
import org.begincode.core.paginator.domain.Paginator;
import org.begincode.core.util.BeginCodePageParam;
import org.begincode.exception.BeginCodeException;
import org.begincode.resource.service.ResourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ResourceServiceImpl
        implements ResourceService {
    private Logger logger = LoggerFactory.getLogger(ResourceServiceImpl.class);

    @Autowired
    ResourceMapper resourceMapper;

    public PageList findResourcesByRecord(Resource record) {
        return this.resourceMapper.selectBySelective(record);
    }

    public Resource findResourceById(int resourceId) {
        Resource record;
        try {
            record = this.resourceMapper.selectByPrimaryKey(Integer.valueOf(resourceId));
        } catch (Exception e) {
            throw new BeginCodeException(e, "resourceId 不存在" + resourceId);
        }
        return record;
    }

    public PageList findResources(Paginator page) {
        BeginCodePageParam.initPaginator(page);
        return this.resourceMapper.selectAll();
    }

    public Resource createResource(Resource record) {
        this.resourceMapper.insertSelective(record);
        return record;
    }

    public PageList findResourcesByRecords(Resource record, Paginator page) {
        BeginCodePageParam.initPaginator(page);
        return this.resourceMapper.selectBySelective(record);
    }

    public void updateAddCountById(Resource record) {
        this.resourceMapper.updateAddCountById(record);
    }

    public void batchUpdateAddCountById(List<Resource> resources) {
        this.resourceMapper.batchUpdateAddCountById(resources);
    }

    public int updateByPrimaryKeySelective(Resource record) {
        return this.resourceMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKeyWithBLOBs(Resource record) {
        return this.resourceMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    public int updateByPrimaryKey(Resource record) {
        return this.resourceMapper.updateByPrimaryKey(record);
    }
}